package org.mch.comm.db.exphandler;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.ResultSetHandler;
import org.mch.comm.utils.MTools;

/**
 * 描述：
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public abstract class ExpRSSQLHandler implements ResultSetHandler<Object>, Serializable {

	private MTools tool = new MTools();

	public MTools tool() {
		return tool;
	}

	public ExpRSSQLHandler(int dbmsType, String tableName) {
		this.dbmsType = dbmsType;
		this.tableName = tableName;
	}

	@Override
	public final Object handle(ResultSet rs) throws SQLException {
		try {
			return handler(rs);
		} catch (Exception e) {
			e.printStackTrace();
			return new SQLException(e);
		}
	}

	public abstract Object handler(ResultSet rs) throws Exception;

	protected String byColTypeColNameGetSQLStr(int coltype, String colname, ResultSet rs) throws SQLException {
		if (rs.getObject(colname) == null)
			return "null";
		switch (coltype) {
			case 4: // '\004'
				return (new StringBuilder(String.valueOf(rs.getInt(colname)))).toString();

			case 1: // '\001'
				return (new StringBuilder("'")).append(rs.getString(colname)).append("'").toString();

			case 12: // '\f'
				return (new StringBuilder("'")).append(rs.getString(colname)).append("'").toString();

			case 8: // '\b'
				return (new StringBuilder(String.valueOf(rs.getDouble(colname)))).toString();

			case 6: // '\006'
				return (new StringBuilder(String.valueOf(rs.getFloat(colname)))).toString();

			case 2: // '\002'
				return (new StringBuilder(String.valueOf(rs.getDouble(colname)))).toString();

			case 3: // '\003'
				return (new StringBuilder(String.valueOf(rs.getDouble(colname)))).toString();

			case 2005:
				return (new StringBuilder("'")).append(rs.getString(colname)).append("'").toString();

			case 5: // '\005'
				return (new StringBuilder(String.valueOf(rs.getInt(colname)))).toString();

			case 93: // ']'
				return "<TIMESTAMP>";

			case -2:
				return "<BINARY>";
		}
		return rs.getString(colname);
	}

	protected String byColTypeGetJavaBeanJavaType(int coltype, int precision, int scale) {
		switch (coltype) {
			case 1: // '\001'
				// if (precision == 19)
				// return "MDateTime";
				// if (precision == 10)
				// return "MDate";
				// if (precision == 8)
				// return "MTime";
				// if (precision == 1)
				// return "MBoolean";
				// else
				return "String";

			case 12: // '\f'
				// if (precision == 10)
				// return "MDate";
				// if (precision == 19)
				// return "MDateTime";
				// if (precision == 8)
				// return "MTime";
				// else
				return "String";

			case 8: // '\b'
				return "Double";

			case 6: // '\006'
				return "Double";

			case 2: // '\002'
				if (scale > 0)
					return "Double";
				if (scale == 0)
					if (precision <= 10)
						return "Integer";
					else
						return "Long";
				// fall through

			case 3: // '\003'
				if (scale > 0)
					return "Double";
				if (scale == 0)
					if (precision <= 10)
						return "Integer";
					else
						return "Long";
				// fall through

			case 4: // '\004'
				return "Integer";

			case 5: // '\005'
				return "Integer";

			case 7: // '\007'
			case 9: // '\t'
			case 10: // '\n'
			case 11: // '\013'
			default:
				return "String";
		}
	}

	protected String byColTypeGetHibernateVOJavaType(int coltype, int precision, int scale) {
		switch (coltype) {
			case 1: // '\001'
				if (precision == 19)
					return "MDateTime";
				if (precision == 10)
					return "MDate";
				if (precision == 8)
					return "MTime";
				if (precision == 1)
					return "MBoolean";
				else
					return "String";

			case 12: // '\f'
				if (precision == 10)
					return "MDate";
				if (precision == 19)
					return "MDateTime";
				if (precision == 8)
					return "MTime";
				else
					return "String";

			case 8: // '\b'
				return "MDouble";

			case 6: // '\006'
				return "MDouble";

			case 2: // '\002'
				if (scale > 0)
					return "MDouble";
				if (scale == 0)
					if (precision <= 10)
						return "Integer";
					else
						return "Long";
				// fall through

			case 3: // '\003'
				if (scale > 0)
					return "MDouble";
				if (scale == 0)
					if (precision <= 10)
						return "Integer";
					else
						return "Long";
				// fall through

			case 4: // '\004'
				return "Integer";

			case 5: // '\005'
				return "Integer";

			case 7: // '\007'
			case 9: // '\t'
			case 10: // '\n'
			case 11: // '\013'
			default:
				return "String";
		}
	}

	protected String byColTypeGetSuperVOJavaType(int coltype, int precision, int scale) {
		switch (coltype) {
			case 1: // '\001'
				if (precision == 19)
					return "UFDateTime";
				if (precision == 10)
					return "UFDate";
				if (precision == 8)
					return "UFTime";
				if (precision == 1)
					return "UFBoolean";
				else
					return "String";

			case 12: // '\f'
				return "String";

			case 8: // '\b'
				return "UFDouble";

			case 6: // '\006'
				return "UFDouble";

			case 2: // '\002'
				if (scale > 0)
					return "UFDouble";
				if (scale == 0)
					if (precision <= 10)
						return "Integer";
					else
						return "Long";
				// fall through

			case 3: // '\003'
				if (scale > 0)
					return "UFDouble";
				if (scale == 0)
					if (precision <= 10)
						return "Integer";
					else
						return "Long";
				// fall through

			case 4: // '\004'
				return "Integer";

			case 5: // '\005'
				return "Integer";

			case 7: // '\007'
			case 9: // '\t'
			case 10: // '\n'
			case 11: // '\013'
			default:
				return "String";
		}
	}

	public String getTableName() {
		return tableName;
	}

	public int getDbmsType() {
		return dbmsType;
	}

	private static final long serialVersionUID = 1L;
	private String tableName;
	private int dbmsType;
}
